from flask import Flask,jsonify,request,Blueprint,session
from common.res import (getRes,getErrorRes,getSuccessRes,getDataRes)
from common.sqlInjectionSecurity import sqlEscape,sqlEscapeField
from DAL.rechargeStatDAL import accountInfoDAL

recharge_account = Blueprint('recharge_account',__name__)

@recharge_account.route("/recharge_account/getlist",methods=['POST'])
def getlist():
    param = {
        'start': request.form.get('start') and sqlEscape(request.form.get('start')) or None,
        'end': request.form.get('end') and sqlEscape(request.form.get('end')) or None,
        'Game': request.form.get('Game') and sqlEscape(request.form.get('Game')) or None,
        'Chan': request.form.get('Chan') and sqlEscape(request.form.get('Chan')) or "",
        'Account': request.form.get('Account') and sqlEscape(request.form.get('Account')) or None,
        'PayUse': request.form.get('PayUse') and sqlEscape(request.form.get('PayUse')) or None,
        'Residue_s': request.form.get('Residue_s') and sqlEscape(request.form.get('Residue_s')) or None,
        'Residue_e': request.form.get('Residue_e') and sqlEscape(request.form.get('Residue_e')) or None,
        'Phone': request.form.get('Phone') and sqlEscape(request.form.get('Phone')) or None,
        'Area': request.form.get('Area') and sqlEscape(request.form.get('Area')) or None,
        'Money_s': request.form.get('Money_s') and sqlEscape(request.form.get('Money_s')) or None,
        'Money_e': request.form.get('Money_e') and sqlEscape(request.form.get('Money_e')) or None,
        'source': request.form.get('source') and sqlEscape(request.form.get('source')) or None,
        'page': request.form.get('page') and sqlEscape(request.form.get('page')) or 1,
        'limit': request.form.get('limit') and sqlEscape(request.form.get('limit')) or 20,
        'filed': request.form.get('filed') and sqlEscape(request.form.get('filed')) or 'id',
        'sort': request.form.get('sort') and sqlEscape(request.form.get('sort')) or 'ascending'
          }
    dal = accountInfoDAL()
    data,count = dal.getList(**param)
    if data:
        res = getSuccessRes(data=data,count=count)
    else:
        res = getSuccessRes(data=[], count=count)
    return jsonify(res)

@recharge_account.route("/recharge_account/getlist4down",methods=['POST'])
def getlist4down():
    param = {
        'start': request.form.get('start') and sqlEscape(request.form.get('start')) or None,
        'end': request.form.get('end') and sqlEscape(request.form.get('end')) or None,
        'Game': request.form.get('Game') and sqlEscape(request.form.get('Game')) or None,
        'Chan': request.form.get('Chan') and sqlEscape(request.form.get('Chan')) or "",
        'Account': request.form.get('Account') and sqlEscape(request.form.get('Account')) or None,
        'PayUse': request.form.get('PayUse') and sqlEscape(request.form.get('PayUse')) or None,
        'Residue_s': request.form.get('Residue_s') and sqlEscape(request.form.get('Residue_s')) or None,
        'Residue_e': request.form.get('Residue_e') and sqlEscape(request.form.get('Residue_e')) or None,
        'Phone': request.form.get('Phone') and sqlEscape(request.form.get('Phone')) or None,
        'Area': request.form.get('Area') and sqlEscape(request.form.get('Area')) or None,
        'Money_s': request.form.get('Money_s') and sqlEscape(request.form.get('Money_s')) or None,
        'Money_e': request.form.get('Money_e') and sqlEscape(request.form.get('Money_e')) or None,
        'source': request.form.get('source') and sqlEscape(request.form.get('source')) or None
          }
    dal = accountInfoDAL()
    data = dal.getlist4down(**param)
    if data:
        res = getSuccessRes(data=data)
    else:
        res = getSuccessRes(data=[])
    return jsonify(res)


@recharge_account.route("/recharge_account/delete_by_id",methods=['POST'])
def delete_by_id():
    param = {
        'id': request.form.get('id') or None
    }
    if not param['id']:
        res = getErrorRes("你输入的参数有错！")
        return jsonify(res)
    dal = accountInfoDAL()
    data = dal.deleteById(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)


@recharge_account.route("/recharge_account/notExistAdd",methods=['POST'])
def notExistAdd():
    param = {
        'Game': request.form.get('Game') and sqlEscape(request.form.get('Game')) or "",
        'Chan': request.form.get('Chan') and sqlEscape(request.form.get('Chan')) or None,
        'Account': request.form.get('Account') and sqlEscape(request.form.get('Account')) or None,
        'Discount': request.form.get('Discount') and sqlEscape(request.form.get('Discount')) or None,
        'PayUse': request.form.get('PayUse') and sqlEscape(request.form.get('PayUse')) or None,
        'Money': request.form.get('Money') and sqlEscape(request.form.get('Money')) or None,
        'Residue': request.form.get('Residue') and sqlEscape(request.form.get('Residue')) or None,
        'source': request.form.get('source') and sqlEscape(request.form.get('source')) or None,
        'Store': request.form.get('Store') and sqlEscape(request.form.get('Store')) or None,
        'Phone': request.form.get('Phone') and sqlEscape(request.form.get('Phone')) or None,
        'device': request.form.get('device') and sqlEscape(request.form.get('device')) or None,
        'Area': request.form.get('Area') and sqlEscape(request.form.get('Area')) or None,
        'Imei': request.form.get('Imei') and sqlEscape(request.form.get('Imei')) or None,
        'Rehcarge_Time': request.form.get('Rehcarge_Time') and sqlEscape(request.form.get('Rehcarge_Time')) or None,
    }
    if not param['Account']:
        res = getErrorRes("充值账号必须填写！")
        return jsonify(res)
    dal = accountInfoDAL()
    data = dal.notExistAdd(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)


@recharge_account.route("/recharge_account/update",methods=['POST'])
def update():
    param = {
        'ID': request.form.get('ID') and sqlEscape(request.form.get('ID')) or None,
        'Game': request.form.get('Game') and sqlEscape(request.form.get('Game')) or "",
        'Chan': request.form.get('Chan') and sqlEscape(request.form.get('Chan')) or "",
        'Account': request.form.get('Account') and sqlEscape(request.form.get('Account')) or None,
        'Discount': request.form.get('Discount') and sqlEscape(request.form.get('Discount')) or 100,
        'PayUse': request.form.get('PayUse') and sqlEscape(request.form.get('PayUse')) or "",
        'Money': request.form.get('Money') and sqlEscape(request.form.get('Money')) or None,
        'Residue': request.form.get('Residue') and sqlEscape(request.form.get('Residue')) or '0',
        'source': request.form.get('source') and sqlEscape(request.form.get('source')) or '',
        'Store': request.form.get('Store') and sqlEscape(request.form.get('Store')) or '',
        'Phone': request.form.get('Phone') and sqlEscape(request.form.get('Phone')) or '',
        'device': request.form.get('device') and sqlEscape(request.form.get('device')) or '',
        'Area': request.form.get('Area') and sqlEscape(request.form.get('Area')) or '',
        'Imei': request.form.get('Imei') and sqlEscape(request.form.get('Imei')) or '',
        'Rehcarge_Time': request.form.get('Rehcarge_Time') and sqlEscape(request.form.get('Rehcarge_Time')) or None
    }
    if not param['ID']:
        res = getErrorRes("参数ID必须填写！")
        return jsonify(res)
    if not param['Account']:
        res = getErrorRes("充值账号必须填写！")
        return jsonify(res)
    dal = accountInfoDAL()
    data = dal.update(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

